package com.why.cachecloud.service.inspect.impl;

import com.why.cachecloud.entity.InstanceInfo;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/**
 * Created by why.developer on 15-1-20.
 */
@Component
public class HostInspectHandler extends AbstractInspectHandler{
    private final static String inspectPoolKey="inspector-host-pool";

    @Resource
    InstanceRunInspector instanceRunInspector;
    @Resource
    RedisIsolationPersistenceInspector redisIsolationPersistenceInspector;

    @PostConstruct
    public void init0(){
        init();
        setInspectorList(Arrays.asList(instanceRunInspector, redisIsolationPersistenceInspector));
    }

    @Override
    public String getThreadPoolKey() {
        return inspectPoolKey;
    }

    @Override
    protected Map<String, List<InstanceInfo>> getSplitMap() {
        List<InstanceInfo> list = getAllInstanceList();
        Map<String, List<InstanceInfo>> hostMap = new TreeMap<String, List<InstanceInfo>>();
        for (InstanceInfo instanceInfo : list) {
            String host = instanceInfo.getIp();
            if (hostMap.containsKey(host)) {
                hostMap.get(host).add(instanceInfo);
            } else {
                List<InstanceInfo> hostInstances = new ArrayList<InstanceInfo>();
                hostInstances.add(instanceInfo);
                hostMap.put(host, hostInstances);
            }
        }
        return hostMap;
    }
}
